package leetcode.huadongchuangkou;

import java.util.Arrays;

public class Test4 {
    public static void main(String[] args) {
        int[] nums = {7, 4, 3, 9, 1, 8, 5, 2, 6};
        int[] averages = getAverages(nums, 3);
        for (int average : averages) {
            System.out.println(average);
        }
    }

    public static int[] getAverages(int[] nums, int k) {
        int[] result = new int[nums.length];
        Arrays.fill(result, -1);
        int n = (2 * k + 1);
        if (nums.length < n) {
            return result;
        }
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
        }
        result[k] = sum / n;
        for (int i = n; i < nums.length; i++) {
            sum += nums[i] - nums[i - n];
            result[i - k] = sum / n;
        }
        return result;
    }
}
